﻿package com.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;

import org.apache.ibatis.session.SqlSession;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.dao.LogDao;
import com.dao.UsersDao;
import com.util.sqlSessionFactory;
@Controller
public class UsersController{
private Map<String, Object> result;
@RequestMapping(value="/register.action",method=RequestMethod.POST)
@ResponseBody
public String register(@RequestParam(value="account",defaultValue="")String  account,
		               @RequestParam(value="password",defaultValue="")String password,
		               @RequestParam(value="password2",defaultValue="")String password2
		){
	SqlSession session = sqlSessionFactory.getSqlSession();
    UsersDao dao = session.getMapper(UsersDao.class);
    LogDao dao1 = session.getMapper(LogDao.class);
    Map<String,Object>result = dao.query(account);
    if(! password.equals(password2)){
	       return "SS";
}
    if ("1".equals(result.get("counter").toString())){
    	return "ERR";
    }
    dao.add(account, password, account, "2016-07-11");
    Map<String, Object> map = dao.lastId();
    dao1.insert(map.get("id").toString(), "注册", "0");
//    Map id = dao1.queryid(account);
//    dao1.insert(id.get("id").toString(), "注册", id.get("id").toString(),"2012-12-12","12:12:12");
    session.commit();
	return "OK";
}
@RequestMapping(value="/login.action",method=RequestMethod.POST)
@ResponseBody
public String login(@RequestParam(value="account",defaultValue="")String account,
		             @RequestParam(value="password",defaultValue="")String password,
		             //@CookieValue(value="user",defaultValue="")String userId,
		             HttpServletResponse response
		             ){
	SqlSession session =sqlSessionFactory.getSqlSession();
	UsersDao dao = session.getMapper(UsersDao.class);
	LogDao dao1 = session.getMapper(LogDao.class);
	Map<String,Object> result = dao.queryone(account, password);
	//System.out.println(userId);
	//session.close();
	if(!"1".equals(result.get("counter").toString())){
		return "ERR";}
	dao1.insert(result.get("id").toString(), "登录", "0");
	response.addCookie(new Cookie("user",result.get("id").toString()));
	session.commit();
	  return "OK";
}
@RequestMapping(value="/quit.action",method=RequestMethod.POST)
@ResponseBody
public String quit(@CookieValue(value="user",defaultValue="")String userId,
                   HttpServletResponse response){
	SqlSession session = sqlSessionFactory.getSqlSession();
	UsersDao dao = session.getMapper(UsersDao.class);
	LogDao dao1 = session.getMapper(LogDao.class);
	Map<String, Object> map = dao.lastId();
	dao1.insert(userId, "退出", map.get("id").toString());
	response.addCookie(new Cookie("user",null));
	session.commit();
	return "OK";
	
}

@RequestMapping(value="/querynickname.action",method=RequestMethod.GET)
@ResponseBody   //当成浏览器 跳过通信  返回浏览器数据
public Map<String,Object> querynickname(
		@CookieValue(value="user",defaultValue="")String userId,
		HttpServletResponse response
		){
//	 if("".equals(userId)){
//     	try{response.sendRedirect("login.html");
//		return null;
//	   }catch(Exception e){
//       System.out.println("[error] getUserInfo.action:redirect\r\n");
//         return null;
//	   }
//	 }
	SqlSession session =sqlSessionFactory.getSqlSession();
	UsersDao dao = session.getMapper(UsersDao.class);
	Map<String,Object> result = dao.querynickname(userId);
	session.close();
	return result;
}
@RequestMapping(value="/queryjiaoyi.action",method=RequestMethod.GET)
@ResponseBody
public List<Map> queryjiaoyi(){
	SqlSession session = sqlSessionFactory.getSqlSession();
	UsersDao dao = session.getMapper(UsersDao.class);
	List<Map> result = dao.queryjiaoyi("date_time","1");
	return result;
}
@RequestMapping(value="/queryjiaoyizh.action",method=RequestMethod.GET)
@ResponseBody
public Map<String, Object> queryjiaoyizh(
		@CookieValue(value="user",defaultValue="")String userId,
		HttpServletResponse response
		){
	SqlSession session = sqlSessionFactory.getSqlSession();
	UsersDao dao = session.getMapper(UsersDao.class);
	Map<String, Object> result = dao.queryjiaoyizh(userId);
	return result;
}
@RequestMapping(value="/queryzuijinjiaoyi.action",method=RequestMethod.GET)
@ResponseBody
 public List<Map> queryzuijinjiaoyi(
		 @CookieValue(value="user",defaultValue="")String userId,
			HttpServletResponse response
		 ){
	 SqlSession session = sqlSessionFactory.getSqlSession();
	 UsersDao dao = session.getMapper(UsersDao.class);
	 List<Map> result = dao.queryzuijinjiaoyi(userId);
	return result;
 }
@RequestMapping(value="/queryzuijinjiaoyizc.action",method=RequestMethod.GET)
@ResponseBody
public List<Map> queryzuijinjiaoyizc(
		@CookieValue(value="user",defaultValue="")String userId,
		HttpServletResponse response
		){
	 SqlSession session = sqlSessionFactory.getSqlSession();
	 UsersDao dao = session.getMapper(UsersDao.class);
	 List<Map> result = dao.queryzuijinjiaoyizc(userId);
			return result;
  }
@RequestMapping(value="/shouru.action",method=RequestMethod.POST)
@ResponseBody
public  String save(@RequestParam(value="money_in",defaultValue="") String money_in,
		    @RequestParam(value="kind",defaultValue="") String kind,
			@RequestParam(value="place",defaultValue="") String place,
		    @RequestParam(value="remark",defaultValue="") String remark,
			@RequestParam(value="date_time",defaultValue="") String date_time,
			
			@CookieValue(value="user",defaultValue="")String userId,
			HttpServletResponse response
			){
			  SqlSession session = sqlSessionFactory.getSqlSession();
		      UsersDao dao = session.getMapper(UsersDao.class);
		      LogDao dao1 = session.getMapper(LogDao.class);
		      dao.save(money_in, kind, place, remark, date_time, userId);
		      Map<String, Object> map = dao.lastId();
		      dao1.insert(userId, "收入", map.get("id").toString());
		      session.commit();
			return "OK";
}

@RequestMapping(value="/zichu.action",method=RequestMethod.POST)
@ResponseBody
public  String savezichu(@RequestParam(value="money_out",defaultValue="") String money_out,
	    @RequestParam(value="kind",defaultValue="") String kind,
		@RequestParam(value="place",defaultValue="") String place,
	    @RequestParam(value="remark",defaultValue="") String remark,
		@RequestParam(value="date_time",defaultValue="") String date_time,
		@CookieValue(value="user",defaultValue="")String userId,
		HttpServletResponse response){
	  SqlSession session = sqlSessionFactory.getSqlSession();
      UsersDao dao = session.getMapper(UsersDao.class);
      LogDao dao1 = session.getMapper(LogDao.class);
      dao.savezichu(money_out, kind, place, remark, date_time, userId);
      Map<String, Object> map = dao.lastId();
      dao1.insert(userId, "支出", map.get("id").toString());
      
      session.commit();
	   return "OK";
}
@RequestMapping(value="/sz.action",method=RequestMethod.POST)
@ResponseBody
public  String sz(
		@RequestParam(value="t", defaultValue="") String t,
		@RequestParam(value="money_out",defaultValue="") String money_out,
		@RequestParam(value="money_in",defaultValue="") String money_in,
	    @RequestParam(value="kind",defaultValue="") String kind,
		@RequestParam(value="place",defaultValue="") String place,
	    @RequestParam(value="remark",defaultValue="") String remark,
		@RequestParam(value="date_time",defaultValue="") String date_time,
		@CookieValue(value="user",defaultValue="")String userId,
		HttpServletResponse response 
		){
	SqlSession session = sqlSessionFactory.getSqlSession();
    UsersDao dao = session.getMapper(UsersDao.class);
    if("money_out".equals(t)){
    	dao.zcsr("0", money_out, kind, place, remark, date_time, userId);
    	//System.out.println("ss");
	       return "ZC";
    }
	if("money_in".equals(t)){
		dao.zcsr(money_in, "0", kind, place, remark, date_time, userId);
	     return "SR";
	}
	session.commit();
	return "";
}

//public static void main(String[] args) {
//	SqlSession session = sqlSessionFactory.getSqlSession();
//	UsersDao dao = session.getMapper(UsersDao.class);
//	List<Map> result = dao.queryzuijinjiaoyi("1");
//	System.out.println(result);
//   }
}
